<template>
	<view class="container">
		<!-- 支付金额 -->
		<view class="amount-section">
			<text class="amount-label">支付金额</text>
			<text class="amount-value">¥{{paymentAmount}}</text>
		</view>

		<!-- 支付方式 -->
		<view class="payment-methods">
			<view class="section-title">选择支付方式</view>
			<view class="method-list">
				<view 
					class="method-item" 
					v-for="(method, index) in paymentMethods" 
					:key="index"
					:class="{ active: selectedMethod === index }"
					@click="selectMethod(index)"
				>
					<view class="method-left">
						<view class="method-icon">{{method.icon}}</view>
						<view class="method-info">
							<text class="method-name">{{method.name}}</text>
							<text class="method-desc">{{method.desc}}</text>
						</view>
					</view>
					<view class="method-right">
						<view class="checkbox" :class="{ checked: selectedMethod === index }"></view>
					</view>
				</view>
			</view>
		</view>

		<!-- 订单信息 -->
		<view class="order-info">
			<view class="section-title">订单信息</view>
			<view class="info-item">
				<text class="info-label">订单号</text>
				<text class="info-value">{{orderInfo.orderNo}}</text>
			</view>
			<view class="info-item">
				<text class="info-label">商品金额</text>
				<text class="info-value">¥{{orderInfo.productAmount}}</text>
			</view>
			<view class="info-item">
				<text class="info-label">运费</text>
				<text class="info-value">¥{{orderInfo.deliveryFee}}</text>
			</view>
			<view class="info-item">
				<text class="info-label">优惠券</text>
				<text class="info-value">-¥{{orderInfo.couponDiscount}}</text>
			</view>
			<view class="info-item total">
				<text class="info-label">实付金额</text>
				<text class="info-value">¥{{orderInfo.totalAmount}}</text>
			</view>
		</view>

		<!-- 底部支付按钮 -->
		<view class="bottom-payment">
			<button class="pay-btn" @click="confirmPayment">立即支付</button>
		</view>

		<!-- 支付密码弹窗 -->
		<view class="password-modal" v-if="showPasswordModal" @click="hidePasswordModal">
			<view class="modal-content" @click.stop>
				<view class="modal-header">
					<text class="modal-title">请输入支付密码</text>
					<view class="close-btn" @click="hidePasswordModal">×</view>
				</view>
				<view class="password-input">
					<input 
						class="password-field" 
						type="number" 
						password="true" 
						maxlength="6"
						v-model="password"
						placeholder="请输入6位支付密码"
					/>
				</view>
				<view class="modal-actions">
					<button class="confirm-btn" @click="submitPassword">确认支付</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Navigation from '@/utils/navigation.js';
	
	export default {
		data() {
			return {
				showPasswordModal: false,
				password: '',
				selectedMethod: 0,
				paymentAmount: '155.56',
				orderInfo: {
					orderNo: 'YT87897079778868686588',
					productAmount: '145.56',
					deliveryFee: '10.00',
					couponDiscount: '0.00',
					totalAmount: '155.56'
				},
				paymentMethods: [
					{ icon: '💳', name: '微信支付', desc: '推荐使用微信支付' },
					{ icon: '💰', name: '支付宝', desc: '安全便捷的支付方式' },
					{ icon: '🏦', name: '银行卡', desc: '支持各大银行储蓄卡' },
					{ icon: '💎', name: '余额支付', desc: '账户余额 ¥99.99' }
				]
			}
		},
		onLoad(options) {
			// 接收从其他页面传递的参数
			if (options.orderId) {
				this.orderInfo.orderNo = options.orderId;
			}
			if (options.amount) {
				this.paymentAmount = options.amount;
				this.orderInfo.totalAmount = options.amount;
			}
			if (options.productAmount) {
				this.orderInfo.productAmount = options.productAmount;
			}
			if (options.deliveryFee) {
				this.orderInfo.deliveryFee = options.deliveryFee;
			}
			if (options.couponDiscount) {
				this.orderInfo.couponDiscount = options.couponDiscount;
			}
		},
		methods: {
			selectMethod(index) {
				this.selectedMethod = index;
			},
			confirmPayment() {
				if (this.selectedMethod === 3) {
					// 余额支付，直接支付
					this.processPayment();
				} else {
					// 其他支付方式，显示密码弹窗
					this.showPasswordModal = true;
				}
			},
			hidePasswordModal() {
				this.showPasswordModal = false;
				this.password = '';
			},
			submitPassword() {
				if (this.password.length !== 6) {
					uni.showToast({
						title: '请输入6位支付密码',
						icon: 'none'
					});
					return;
				}
				
				this.hidePasswordModal();
				this.processPayment();
			},
			processPayment() {
				uni.showLoading({
					title: '支付中...'
				});
				
				// 模拟支付过程
				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '支付成功',
						icon: 'success'
					});
					
					// 跳转到支付成功页面
					setTimeout(() => {
						Navigation.goToSuccess({
							orderId: this.orderInfo.orderNo,
							amount: this.paymentAmount
						});
					}, 1500);
				}, 2000);
			}
		}
	}
</script>

<style scoped>
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
	padding-bottom: 120rpx;
}

/* 支付金额 */
.amount-section {
	background-color: #fff;
	padding: 60rpx 30rpx;
	text-align: center;
	margin-bottom: 20rpx;
}

.amount-label {
	font-size: 28rpx;
	color: #666;
	display: block;
	margin-bottom: 20rpx;
}

.amount-value {
	font-size: 60rpx;
	color: #ff4757;
	font-weight: bold;
}

/* 支付方式 */
.payment-methods {
	background-color: #fff;
	margin-bottom: 20rpx;
	padding: 30rpx;
}

.section-title {
	font-size: 32rpx;
	color: #333;
	font-weight: bold;
	margin-bottom: 30rpx;
}

.method-list {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.method-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 25rpx;
	border: 2rpx solid #f0f0f0;
	border-radius: 15rpx;
	transition: all 0.3s;
}

.method-item.active {
	border-color: #ff6b35;
	background-color: #fff5f0;
}

.method-left {
	display: flex;
	align-items: center;
	gap: 20rpx;
}

.method-icon {
	font-size: 50rpx;
}

.method-info {
	display: flex;
	flex-direction: column;
	gap: 8rpx;
}

.method-name {
	font-size: 28rpx;
	color: #333;
	font-weight: bold;
}

.method-desc {
	font-size: 24rpx;
	color: #999;
}

.method-right {
	display: flex;
	align-items: center;
}

.checkbox {
	width: 40rpx;
	height: 40rpx;
	border: 2rpx solid #ddd;
	border-radius: 50%;
	position: relative;
}

.checkbox.checked {
	border-color: #ff6b35;
	background-color: #ff6b35;
}

.checkbox.checked::after {
	content: '✓';
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	color: white;
	font-size: 24rpx;
}

/* 订单信息 */
.order-info {
	background-color: #fff;
	margin-bottom: 20rpx;
	padding: 30rpx;
}

.info-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 20rpx 0;
	border-bottom: 1rpx solid #f0f0f0;
}

.info-item:last-child {
	border-bottom: none;
}

.info-item.total {
	border-top: 1rpx solid #f0f0f0;
	margin-top: 20rpx;
	padding-top: 20rpx;
}

.info-label {
	font-size: 26rpx;
	color: #666;
}

.info-value {
	font-size: 26rpx;
	color: #333;
}

.info-item.total .info-value {
	color: #ff4757;
	font-weight: bold;
	font-size: 28rpx;
}

/* 底部支付按钮 */
.bottom-payment {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background-color: #fff;
	padding: 20rpx 30rpx;
	border-top: 1rpx solid #f0f0f0;
}

.pay-btn {
	width: 100%;
	background-color: #ff6b35;
	color: white;
	border: none;
	padding: 25rpx;
	border-radius: 25rpx;
	font-size: 32rpx;
	font-weight: bold;
}

/* 支付密码弹窗 */
.password-modal {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 1000;
	display: flex;
	align-items: center;
	justify-content: center;
}

.modal-content {
	background-color: #fff;
	width: 80%;
	border-radius: 20rpx;
	padding: 40rpx;
}

.modal-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 40rpx;
}

.modal-title {
	font-size: 32rpx;
	color: #333;
	font-weight: bold;
}

.close-btn {
	font-size: 40rpx;
	color: #999;
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.password-input {
	margin-bottom: 40rpx;
}

.password-field {
	width: 100%;
	padding: 25rpx;
	border: 2rpx solid #f0f0f0;
	border-radius: 15rpx;
	font-size: 32rpx;
	text-align: center;
	letter-spacing: 10rpx;
}

.modal-actions {
	text-align: center;
}

.confirm-btn {
	background-color: #ff6b35;
	color: white;
	border: none;
	padding: 25rpx 60rpx;
	border-radius: 25rpx;
	font-size: 32rpx;
	font-weight: bold;
}
</style>